/**
 * 
 */
package com.testing.mq.rabbitmq;


/**
 * RabbitMQ producer
 *
 * @author Jason
 * created at: Sep 26, 2011
 */
public class SimpleMQProducer extends AbstractSimpleMQWorker {
	
	public SimpleMQProducer() {
		super();
	}
	public SimpleMQProducer(String pQHost, String pQName) {
		super(pQHost, pQName);
	}
	
	/**
	 * just send the given message
	 * @param pMessage
	 */
	public void produceMessage(final String pMessage) {
		if (pMessage!=null && !pMessage.isEmpty()) {
			try {
				/*
				 * exchange = in case you have, if you don't, use "" instead
				 * routingKey = queueName for DIRECT Q(s)
				 */
				this.qChannel.basicPublish("", this.qName, null, pMessage.getBytes());
				
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException("[SimpleMQProducer][produceMessage]", e);
			}
		}	
	}
	
	/**
	 * cleanup
	 */
	public void dispose() {
		if (this.qChannel!=null) try { this.qChannel.close(); } catch(Exception e) {}
		if (this.qConnection!=null) try { this.qConnection.close(); } catch(Exception e) {}
	}
	
}
